<template>
  <el-dialog
    title="邮件配置"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="SMTP" prop="smtp">
        <el-input v-model="dataForm.smtp" placeholder="SMTP"></el-input>
      </el-form-item>
      <el-form-item label="端口号" prop="port">
        <el-input v-model="dataForm.port" placeholder="端口号"></el-input>
      </el-form-item>
      <el-form-item label="邮箱账号" prop="username">
        <el-input v-model="dataForm.username" placeholder="邮箱账号"></el-input>
      </el-form-item>
      <el-form-item label="邮箱密码" prop="password">
        <el-input v-model="dataForm.password" placeholder="邮箱密码"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          smtp: '',
          port: '',
          username: '',
          password: ''
        },
        dataRule: {
          smtp: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          port: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init () {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.getConfig()
        })
      },
      // 获取邮件配置
      getConfig () {
        this.$http.get(`${window.SITE_CONFIG['baseURL']}/sys/mailtemplate/config`).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.smtp = data.config.smtp
            this.dataForm.port = data.config.port
            this.dataForm.username = data.config.username
            this.dataForm.password = data.config.password
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http.post(
              `${window.SITE_CONFIG['baseURL']}/sys/mailtemplate/saveConfig`,
              this.dataForm
            ).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
